package com.noble.glamour.module.local.chat;


import android.app.Activity;

import com.faceunity.MRender;
import com.noble.glamour.module.local.chat.msgtype.bean.BaseMessage;
import com.noble.glamour.module.local.chat.msgtype.BaseMessageType;
import com.noble.glamour.module.local.chat.msgtype.type.InviteVideoMessage;
import com.noble.glamour.module.local.chat.msgtype.type.SystemMessage;
import com.noble.glamour.module.local.chat.msgtype.type.VideoMessage;
import com.noble.glamour.module.local.chat.msgtype.type.VideoOptiMessage;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.invoke.Invoker;
import com.noble.glamour.module.util.CountDownTimerUtil;
import com.noble.glamour.ui.agora.manager.RtcMessageMgr;
import com.noble.library.log.PSP;

/**
 * 特殊消息处理类，减轻重度依赖
 * Created by Kind on 2017/8/22.
 */

public class SpecialMsgMgr {


    public void init() {
    }

    public void release() {
    }


    public void setSpecialMsg(BaseMessage message) {
        switch (message.getType()) {
            case BaseMessageType.video_MsgType://视频消息
                setVideoMsg(message);
                break;
            case BaseMessageType.System_MsgType://系统消息
                setSystemMsg(message);
                break;
            case BaseMessageType.Recved_MsgType://送达消息
                setMsgRecvedType(message);
                break;
            case BaseMessageType.inviteVideoDelivery_MsgType://语音(视频)邀请送达男用户
                setInviteVideoDelivery(message);
                break;
            case BaseMessageType.Alone_Invite_Video_MsgType://女用户单独视频邀请
                setAloneInviteVideoMsg(message);
                break;
            case BaseMessageType.VIDEOCHAT_COMPLAIN__MsgType: // 视频聊天中的投诉消息
                setComlpainMsg(message);
                break;
            case BaseMessageType.VIDEOCHAT_FACEU__MsgType:  // 视频聊天中的道具消息
                setFaceUMsg(message);
                break;
            default:
                break;
        }
    }

    /**
     * 视频聊天中的道具消息
     */
    private void setFaceUMsg(BaseMessage message) {
        if (message == null) return;
        VideoOptiMessage faceUMsg = new VideoOptiMessage();
        faceUMsg.parseFaceU(message.getJsonStr());
        // 非本次通话，不做处理
        if (faceUMsg.getVc_id() != ModuleMgr.getRtcEnginMgr().getEngineConfig().mVcid) return;
        MRender.setCurrentItemByPosition(faceUMsg.getFaceu_id());
    }

    /**
     * 视频聊天中投诉消息
     */
    private void setComlpainMsg(BaseMessage message) {
        if (message == null) return;
        VideoOptiMessage comlpainMsg = new VideoOptiMessage();
        comlpainMsg.parseFaceU(message.getJsonStr());
        // 视频通信时不处理其他用户消息
        if (ModuleMgr.getRtcEnginMgr().getEngineConfig().mOtherId != comlpainMsg.getFid()) return;
        Invoker.getInstance().warningMessageJSCMD(comlpainMsg.getFid(), comlpainMsg.getTid(), comlpainMsg.getMsgDesc());
    }

    /**
     * 音视频消息
     *
     * @param message
     */
    private void setVideoMsg(BaseMessage message) {
        if (message == null) return;
        LoadingDialog.closeLoadingDialog();
        final VideoMessage videoMessage = (VideoMessage) message;
        RtcMessageMgr.getInstance().addVideoMsg(videoMessage);  // 保存当前消息

        if (videoMessage.getVideoTp() == 1) {
            // 女性用户且处于群发状态, 直接打开聊天界面
            if (!ModuleMgr.getCenterMgr().getMyInfo().isMan() && ModuleMgr.getRtcChatMgr().getGroupInviteStatus()) {
                ModuleMgr.getRtcChatMgr().setGroupInviteStatus(false);
                RtcMessageMgr.getInstance().sendGroupAcceptMsg(videoMessage);
                return;
            }
            ModuleMgr.getRtcChatMgr().openInvitedActivity((Activity) App.getActivity(),
                    videoMessage.getVideoID(), videoMessage.getLWhisperID(), videoMessage.getVideoMediaTp(), 0);
        } else {
            boolean isInvite = PSP.getInstance().getBoolean("ISINVITE", false);  // 男号私聊页立即接听时为true， 其他全部为false
            ModuleMgr.getRtcEnginMgr().getEngineConfig().mVcid = videoMessage.getVideoID();
            ModuleMgr.getRtcEnginMgr().getEngineConfig().mChannel = String.valueOf(videoMessage.getVideoID());
            if (isInvite && videoMessage.getVideoTp() == 2) { // 男号点立即接听时收到女性同意消息后，直接开启聊天页面
                CountDownTimerUtil.getInstance().addHandledIds(ModuleMgr.getRtcChatMgr().getInviteTime());
                ModuleMgr.getRtcChatMgr().openInvitedDirect((Activity) App.getActivity(),
                        videoMessage.getVideoID(), videoMessage.getLWhisperID(), videoMessage.getVideoMediaTp(), videoMessage.getVc_channel_key());
                return;
            }
            RtcMessageMgr.getInstance().sendVideoMsg(videoMessage.getVideoTp(), videoMessage.getVc_channel_key());
        }
    }

    /**
     * 男用户： 女性单独音视频邀请消息
     *
     * @param message
     */
    private void setAloneInviteVideoMsg(BaseMessage message) {
        if (message == null) return;
        InviteVideoMessage videoMessage = new InviteVideoMessage();
        videoMessage.parseJs(message.getJsonStr());
        RtcMessageMgr.getInstance().addVideoMsg(videoMessage);  // 保存当前消息
//        if (ModuleMgr.getCenterMgr().getMyInfo().getDiamand() < videoMessage.getPrice()) {
//
//            //充值弹框
//            UIShow.showBottomChatDiamondDlg(App.getActivity(), message.getLWhisperID(), videoMessage.getMedia_tp(),
//                    (int) videoMessage.getPrice(), true, videoMessage.getInvite_id());
//            return;
//        }

        //跳转视频
        ModuleMgr.getRtcChatMgr().openInvitedActivity((Activity) App.getActivity(),
                videoMessage.getInvite_id(), message.getLWhisperID(), videoMessage.getMedia_tp(), videoMessage.getPrice());
    }

    /**
     * 系统消息
     *
     * @param message
     */
    private void setSystemMsg(BaseMessage message) {
        if (message == null) return;
        SystemMessage mess = new SystemMessage();
        mess.parseJson(message.getJsonStr());
        switch (mess.getXtType()) {
            case 3:
                ModuleMgr.getChatMgr().updateOtherRead(null, mess.getFid() + "", mess.getTid(), message);
                break;
        }
    }

    /**
     * 送达消息Msg_RecvedType
     *
     * @param message
     */
    private void setMsgRecvedType(BaseMessage message) {
        if (message == null) return;
        SystemMessage mess = new SystemMessage();
        mess.parseJson(message.getJsonStr());
        ModuleMgr.getChatMgr().updateDeliveryStatus(mess.getMsgID(), null);
    }

    /**
     * 男用户： 语音(视频)群邀消息
     *
     * @param message
     */
    private void setInviteVideoDelivery(BaseMessage message) {
        if (message == null || !(message instanceof InviteVideoMessage))
            return;

        InviteVideoMessage mInviteVideoMessage = (InviteVideoMessage) message;
        CountDownTimerUtil util = CountDownTimerUtil.getInstance();
        long timeCount = mInviteVideoMessage.getTimeout_tm() - ModuleMgr.getAppMgr().getSecondTime();//计时时间
        if (timeCount > 120) {
            timeCount = 120;
        }
        if (timeCount > 0 && !util.isTimingTask(mInviteVideoMessage.getInvite_id()) && !util.isTimingTask(mInviteVideoMessage.getInvite_id())) {
            util.addTimerTask(mInviteVideoMessage.getTime(), timeCount);
        }
    }
}
